package com.axjy.util;

import com.axjy.pojo.BasePojo;
import com.axjy.pojo.User;
import org.springframework.aop.MethodBeforeAdvice;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * Created by dell on 2018/1/10.
 */
public class BasePojoAcive  implements MethodBeforeAdvice{

    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        BasePojo basePjo= null;
        if(objects != null&&objects[0] != null&&objects[0] instanceof BasePojo){
            basePjo =(BasePojo) objects[0];
            basePjo.setCreationDate(LocalDateTime.now());
        }
        if (objects != null&&objects[1] != null&&objects[1] instanceof HttpSession){
            HttpSession session = (HttpSession) objects[1];
            User loginUser = (User) session.getAttribute("loginUser");
            basePjo.setCreatedBy(loginUser.getCreatedBy());
        }

    }
}
